package git.soulbgm.security.service.impl;

import git.soulbgm.common.pojo.PageInfo;
import git.soulbgm.pojo.vo.OnlineUserInfo;
import git.soulbgm.security.pojo.LoginUser;
import git.soulbgm.security.service.OnlineUserService;
import git.soulbgm.security.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 使用Token的在线用户服务
 *
 * @author SoulBGM
 * @date 2021-09-08
 */
@Service("tokenOnlineUserService")
public class TokenOnlineUserServiceImpl extends OnlineUserService {

    @Autowired
    private TokenService tokenService;

    @Override
    public List<OnlineUserInfo> getOnlineUser(String username, String loginIpAddress) {
        List<LoginUser> list = tokenService.getAllLoginUser();
        List<OnlineUserInfo> onlineUserInfoList = new ArrayList<>(list.size());
        for (LoginUser loginUser : list) {
            addOnlineUserInfo(username, loginIpAddress, onlineUserInfoList, loginUser.getToken(), loginUser);
        }
        return onlineUserInfoList;
    }

    @Override
    public boolean forcedOfflineUser(List<String> sessionList) {
        try {
            for (String s : sessionList) {
                String[] split = s.split(":");
                String token = split[0];
                String username = split[1];
                tokenService.delLoginUser(username, token);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
